from rest_framework.viewsets import ModelViewSet
from aaa import models
from rest_framework import serializers
from rest_framework.response import Response
import json
from django.http import JsonResponse
from aaa.utils.pager import myPageNumberPagination

class goodsproductSerializer(serializers.ModelSerializer):
    class Meta:
        model = models.GoodsProduct
        fields = ['id','goods','specifications','price','number','goods_image']

class goodsproductmodeView(ModelViewSet):
    queryset = models.GoodsProduct.objects.all()
    serializer_class = goodsproductSerializer
    # pagination_class = PageNumberPagination
    authentication_classes = []

    def list(self, request, *args, **kwargs):
        print('args',request._request.GET.get('pagenum'))
        # goods = models.Categories.objects.all()
        pg = myPageNumberPagination()
        xx = pg.paginate_queryset(queryset=self.queryset,request=request,view=self)
        ser = goodsproductSerializer(instance=xx,many=True)
        ret = {}
        ret['data'] = json.dumps(ser.data)
        print('ser',len(ret['data']),ret['data'])
        ret['total'] = self.queryset.count()
        return JsonResponse(ret)

    # def create(self, request, *args, **kwargs):
    #     print('add goodsproduct res',request.data)
    #     data = request.data
    #     ['id', 'goods', 'specifications', 'price', 'number', 'goods_image']
    #     try:
    #         obj = models.GoodsProduct.objects.create(goods_id=data['goods'][0],specifications=data['specifications'],
    #                                           price=data['price'],number=data['number'],
    #                                           goods_image=data['goods_image'])
    #         return JsonResponse({'meta': {'status': 201, 'msg': 'ok'}})
    #     except Exception as e:
    #         print('e',e)
    #
    # def update(self, request, *args, **kwargs):
    #     print('update goodsproduct res', request.data)
    #     data = request.data
    #     try:
    #         obj = models.GoodsProduct.objects.filter(goods=data['goods'][0]).update(
    #             goods=data['goods'], specifications=data['specifications'],
    #             price=data['price'], number=data['number'],
    #             goods_image=data['goods_image'])
    #         return JsonResponse({'meta': {'status': 201, 'msg': 'ok'}})
    #     except Exception as e:
    #         print('e',e)